package core.scheduledtask;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.TimerTask;

import org.apache.log4j.Logger;

import protocol.iec104.IFrameStruct;
import task.iec104.IEC104SendTask;
import util.DataUtil;
import util.MyByteArrayOutputStream;


import core.IEC104Server;
import core.ProcessThread;
import dbop.DBManager;

public class getFBDataTask extends TimerTask {
	
	private ProcessThread thd;
	private static Logger logger = Logger.getLogger(getFBDataTask.class);

	public getFBDataTask(ProcessThread thd) {
		// TODO Auto-generated constructor stub
		this.thd=thd;
	}
	
	@Override
	public void run() {
		// TODO Auto-generated method stub
//		try {
//			getFBYXdata();
//			getFBYCdata();
//		} catch (SQLException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		genfbdata();
	}
	private void genfbdata(){
		for (int i=100;i<200;i++){
			int v=Integer.parseInt(IEC104Server.yxdata.get(String.valueOf(i)));
			if (v>0){
				IEC104Server.yxdata.put(String.valueOf(i), "0");
				IFrameStruct ist= new IFrameStruct(thd.getSendIndex(), thd.getRcvIndex(), 1,1,3,1,i,new byte[]{(byte)0});
				IEC104SendTask task= new IEC104SendTask(thd, ist);
				IEC104Server.worker.feedHiTask(task);
			}else{
				IEC104Server.yxdata.put(String.valueOf(i), "1");
				IFrameStruct ist= new IFrameStruct(thd.getSendIndex(), thd.getRcvIndex(), 1,1,3,1,i,new byte[]{(byte)1});
				IEC104SendTask task= new IEC104SendTask(thd, ist);
				IEC104Server.worker.feedHiTask(task);
			}
			
		}
		for (int j=11785;j<11800;j++){
			Float f= Float.parseFloat(IEC104Server.ycdata.get(String.valueOf(j)));
			f=f+1;
			IEC104Server.ycdata.put(String.valueOf(j), String.valueOf(f));
			MyByteArrayOutputStream ouput= new MyByteArrayOutputStream();
			try {
				ouput.write(DataUtil.float2byte(f));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			ouput.write(0);
			byte[] data= ouput.toByteArray();
			IFrameStruct ist= new IFrameStruct(thd.getSendIndex(), thd.getRcvIndex(), 21,1,3,1,j,data);
			IEC104SendTask task= new IEC104SendTask(thd, ist);
			IEC104Server.worker.feedTask(task);
			
			
		}
		
	}
	
	private void getFBYXdata() throws SQLException{
		logger.info("get fb yx data");
		
		Connection conn= DBManager.getmysqlConn("DB.MYSQL");
		if (conn!=null){
			Statement stmt = conn.createStatement();
			
			String sql= "SELECT * from vw_yxdata order by vw_yxdata.FINT_INFO  asc ";
			
			boolean result = stmt.execute(sql);
			if (result) {
				
				ResultSet res = stmt.getResultSet();
				int count=0;
				while (res.next()) {
					count++;
					String point            =res.getString(1);
					String value      =res.getString(2);
					String vold= IEC104Server.yxdata.get(point);
					if (vold!=null){
						if (!vold.equals(value)){
							logger.info("fb yx: "+point+" "+value);
							IEC104Server.yxdata.put(point, value);
							IFrameStruct ist= new IFrameStruct(thd.getSendIndex(), thd.getRcvIndex(), 1,1,3,1,Integer.parseInt(point),new byte[]{(byte)Integer.parseInt(value)});
							IEC104SendTask task= new IEC104SendTask(thd, ist);
							IEC104Server.worker.feedHiTask(task);
						}
						
						
					}
					
					
					
					
					
				}
				
				res.close();
			}
			stmt.close();
		}
		conn.close();
	}
	private void getFBYCdata() throws SQLException{
		logger.info("get fb yc data");
		
		Connection conn= DBManager.getmysqlConn("DB.MYSQL");
		if (conn!=null){
			Statement stmt = conn.createStatement();
			
			String sql= "SELECT * from vw_ycdata order by vw_ycdata.FINT_INFO  asc ";
			
			boolean result = stmt.execute(sql);
			if (result) {
				
				ResultSet res = stmt.getResultSet();
				int count=0;
				while (res.next()) {
					count++;
					String point            =res.getString(1);
					String value      =res.getString(2);
					String vold= IEC104Server.ycdata.get(point);
					if (vold!=null){
						if (!vold.equals(value)){
							logger.info("fb yc: "+point+" "+value);
							IEC104Server.ycdata.put(point, value);
							float fv= Float.parseFloat(value);
							MyByteArrayOutputStream ouput= new MyByteArrayOutputStream();
							try {
								ouput.write(DataUtil.float2byte(fv));
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							ouput.write(0);
							byte[] data= ouput.toByteArray();
							IFrameStruct ist= new IFrameStruct(thd.getSendIndex(), thd.getRcvIndex(), 21,1,3,1,Integer.parseInt(point),data);
							IEC104SendTask task= new IEC104SendTask(thd, ist);
							IEC104Server.worker.feedTask(task);
						}
						
						
					}
					
					
					
					
					
				}
				
				res.close();
			}
			stmt.close();
		}
		conn.close();
	}
	
	private void SendFbData(){
		for (String addr:IEC104Server.yxFBdata.keySet()){
			//String v= IEC104.yxdata
			byte[] data;
	    	//MyByteArrayOutputStream output = new MyByteArrayOutputStream();
			//output.reset();
			int iaddr= Integer.parseInt(addr);
			//data= 
				
			//IFrameStruct ist= new IFrameStruct(thd.getSendIndex(), thd.getRcvIndex(), 100,1,3,1,iaddr,data);
			
		}
	}

}
